package com.controller;

import com.alibaba.fastjson.JSON;
import com.entity.*;
import com.service.DepartmentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentservice;

    @RequestMapping("/getDepartment")
    public List<Department> getDepartment(){
        return departmentservice.findAll();
    }

    @RequestMapping("/addDepartment")
    public String addDepartment(@RequestBody Department department){
        int i=departmentservice.addDepartment(department);
        return i>0?"success":"error";
    }

    @RequestMapping("/deleteDepartment")
    public String deleteDepartment(String dpnum){
        int i=departmentservice.deleteDepartment(dpnum);
        return i > 0 ? "error":"success";
    }

    @RequestMapping("/editDepartment")
    public String editDepartment (@RequestBody Department department){
        int i=departmentservice.editDepartment(department);
        return i>0?"success":"error";
    }

    @RequestMapping("/upDepartment")
    public String upDepartment(String dpnum){
        Department department=departmentservice.upDepartment(dpnum);
        String string = JSON.toJSONString(department);
        return string;
    }
}
